package cli.menu;
/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * RenamePaymentMode Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by Tan Si Ling (A0085314J)
 */

//modify payment mode needs to be supported by API : check for duplicate naming.


import cli.tools.Process;
import shared.*;

class RenamePaymentMode extends Function {

	private static final String[] SELECTCAT = {
			"-= Main menu > Manage Categories > Rename a Payment Mode =-",
			"[b]: back [u]: undo [r]: redo [h]: home menu", "",
			"Select a Payment Mode by its index" };

	private static final String[] ENTERNEWNAME = { "Enter the new name for this Payment Mode: " };

	private static final State BACK = State.MANAGEPAYMENTMODES;
	private static final State CURRENT = State.RENAMEPAYMENTMODE;

	
	public static State runMenu() {

		cli.print(SELECTCAT);
		Object choice = tools.selectFromList(cli.getApi().getModeList(),
				"Modify Payment Modes");

		// if back signal sent
		if (choice == null) {
			return BACK;
		}

		// if received Instruction object (divert to another menu)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// last case : if choice was really the correct object
	Mode modeToRename = (Mode) choice;


	if (modeToRename.equals(cli.getApi().getModeList().get(0))) {
		cli.println("Unable to rename this Mmde of payment");
		cli.println("Returning to previous menu");
		return State.MANAGEPAYMENTMODES;
	}

		cli.print(ENTERNEWNAME);
	
		String input = cli.nextLine();

		//TODO - potential problem here
		// Si Ling: **Keeping Track of the Actions DONE**
		tools.action.addToUndoStack(modeToRename.getName(), input,
				Process.MODIFYPAYMENTMODENAME, modeToRename);

		// **END**
		

		//modeToRename.modifyName(input);
		try {
			cli.getApi().modifyMode(modeToRename, input);
		} catch (Exception e) {
			cli.print(ERROR);
		}
		// return to parent menu
		return CURRENT;
	}

}